package org.dce.screencapturer.editors;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.dce.screencapturer.objects.ShadowAttributes;
import org.dce.screencapturer.objects.ShapeObject;

@SuppressWarnings("serial")
public class ShadowEditor 
	extends Editor{

	private JCheckBox enabled = new JCheckBox("Enabled?");
	private JSlider xOffset = new JSlider(-20,20,0);
	private JSlider yOffset = new JSlider(-20,20,0);
	private JSlider transparency = new JSlider(0,100,0);
	private final SampleView sampleView = new SampleView();
	
	public ShadowEditor(ShapeObject editedObject) {
		super(editedObject, "Edit Shadow");
		
		JPanel panel = new JPanel(new GridBagLayout());

		panel.add(enabled, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));

		panel.add(new JLabel("Offset in X"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));

		panel.add(xOffset, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
		
		panel.add(new JLabel("Offset in Y"), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));

		panel.add(yOffset, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));

		panel.add(new JLabel("Transparency"), new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));

		panel.add(transparency, new GridBagConstraints(1, 3, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));

		sampleView.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Sample"));

		panel.add(sampleView, new GridBagConstraints(0, 4, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));

		add(panel,BorderLayout.CENTER);
		
		final ChangeListener listener = new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				sampleView.repaint();
			}
		};
		
		xOffset.addChangeListener(listener);
		yOffset.addChangeListener(listener);
		transparency.addChangeListener(listener);
		
	}

	@Override
	protected void readFromObject() {
		
		ShadowAttributes shadowAttributes = editedObject.getShadowAttributes();
		
		enabled.setSelected(shadowAttributes.isEnabled());
		xOffset.setValue(shadowAttributes.getOffsetX());
		yOffset.setValue(shadowAttributes.getOffsetY());
		transparency.setValue(shadowAttributes.getTransparency());
	}

	@Override
	protected void writeToObject() {
		
		ShadowAttributes shadowAttributes = (ShadowAttributes)editedObject.getShadowAttributes().clone();

		shadowAttributes.setEnabled(enabled.isSelected());
		shadowAttributes.setOffsetX(xOffset.getValue());
		shadowAttributes.setOffsetY(yOffset.getValue());
		shadowAttributes.setTransparency(transparency.getValue());
		
		editedObject.setShadowAttributes(shadowAttributes);
		
	}
	
	private class SampleView extends JComponent {
		
		
		protected void paintComponent(Graphics g){
			
			Graphics2D g2d = (Graphics2D)g;
			
			Composite c = g2d.getComposite();
			
			g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(1f - transparency.getValue()/100f)));
		
			int xOffset = ShadowEditor.this.xOffset.getValue();
			int yOffset = ShadowEditor.this.yOffset.getValue();
			
			g2d.translate(xOffset,yOffset);
			
			int x = (getWidth() - 150)/2;
			int y = (getHeight() - 100)/2;
			
			g2d.setColor(Color.BLACK);
			g2d.fillRect(x, y, 150, 100);
			
			g2d.setComposite(c);
			
			g2d.translate(-xOffset,-yOffset);
			
			g2d.setColor(Color.WHITE);
			g2d.fillRect(x, y, 150, 100);
			g2d.setColor(Color.BLACK);
			g2d.drawRect(x, y, 150, 100);
			
		}
	}

}
